package com.nhjf.pojo.dto.result;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import com.nhjf.exception.CzshopException;

/**
 * 服务通用返回DTO
 * 
 * @author zhurz
 */
@SuppressWarnings("rawtypes")
public class ServiceResultDto<T> implements Serializable {
	private static final long serialVersionUID = 1L;
	
	private final static String UNIQUE_VALUE_KEY = "UNIQUE_VALUE";
	/**
	 * 是否成功
	 */
	private Boolean success = false;
	/**
	 * 返回代码
	 */
	private String resultCode;
	/**
	 * 返回消息
	 */
	private String resultMessage;
	/**
	 * 返回的数据对象
	 */
	private Map<String, Object> resultDataMap;
	/**
	 * 异常信息
	 */
	private CzshopException czshopException;
	
	public void setUniqueResult(T object){
		getResultDataMap().put(UNIQUE_VALUE_KEY, object);
	}
	
	/**
	 * 得到唯一结果
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public T getUniqueResult() {
		return (T) getResultDataMap().get(UNIQUE_VALUE_KEY);
	}

	public static ServiceResultDto successVoid() {
		ServiceResultDto dto = new ServiceResultDto();
		dto.setSuccess(true);
		return dto;
	}

	@SuppressWarnings("unchecked")
	public static ServiceResultDto success(Object object) {
		ServiceResultDto dto = new ServiceResultDto();
		dto.setSuccess(true);
		dto.setUniqueResult(object);
		return dto;
	}

	public static ServiceResultDto failVoid() {
		return fail(null);
	}
	
	public static ServiceResultDto fail(String message) {
		return fail(message, null);
	}
	
	public static ServiceResultDto fail(String message, String resultCode) {
		ServiceResultDto dto = new ServiceResultDto();
		dto.setResultCode(resultCode);
		dto.setSuccess(false);
		dto.setResultMessage(message);
		return dto;
	}

	public Boolean getSuccess() {
		return success;
	}

	public void setSuccess(Boolean success) {
		this.success = success;
	}

	public String getResultCode() {
		return resultCode;
	}

	public void setResultCode(String resultCode) {
		this.resultCode = resultCode;
	}

	public String getResultMessage() {
		return resultMessage;
	}

	public void setResultMessage(String resultMessage) {
		this.resultMessage = resultMessage;
	}

	public CzshopException getCzshopException() {
		return czshopException;
	}

	public void setCzshopException(CzshopException czshopException) {
		this.czshopException = czshopException;
	}

	public Map<String, Object> getResultDataMap() {
		if(resultDataMap == null){
			resultDataMap = new HashMap<String, Object>();
		}
		return resultDataMap;
	}

	public void setResultDataMap(Map<String, Object> resultDataMap) {
		this.resultDataMap = resultDataMap;
	}
}
